#include <unistd.h>
#include "basesvr.hpp"

IMPL_LOGGER(BaseSvr, logger);

int BaseSvr::init(int argc, char ** argv)
{
	// BaseSvr::init call Runner::init
	// Runner::init callback BaseSvr::runner_init
	// Balance::runner_init call BaseSvr::init_impl

	_runner._init       = BaseSvr::runner_init;
	_runner._proc       = BaseSvr::runner_proc;
	_runner._reload     = BaseSvr::runner_reload;
	_runner._finish     = BaseSvr::runner_finish;

	return _runner.init((void*)this, argc, argv);
}

int BaseSvr::runner_init(void * pThis, int argc, char ** argv)
{
	assert(NULL != pThis);
	
	BaseSvr * pSvr = (BaseSvr*)pThis;

	std::string log = pSvr->_runner._log4cplus;

	if (log.empty())
	{
		log = "../conf/logger.properties";
	}

	if (access(log.c_str(), F_OK) != 0)
	{
		assert(false);
	}

	log4cplus::PropertyConfigurator::doConfigure(log);

	return pSvr->impl_init();
}

int BaseSvr::runner_proc(void * pThis)
{
	assert(NULL != pThis);
	
	BaseSvr * pSvr = (BaseSvr*)pThis;
	
	return pSvr->process();

	return 0;
}

int BaseSvr::runner_reload(void * pThis)
{
	assert(NULL != pThis);
	
	BaseSvr * pSvr = (BaseSvr*)pThis;
	
	return pSvr->reload();
}

int BaseSvr::runner_finish(void * pThis)
{
	assert(NULL != pThis);
	
	BaseSvr * pSvr = (BaseSvr*)pThis;

	return pSvr->finish();
}

